package ar.edu.isft189.dao;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.List;

import org.firebirdsql.jdbc.FirebirdCallableStatement;

import ar.edu.isft189.model.Docente;
import ar.edu.isft189.utility.DBUtility;

public class DocenteCrudDao {
	private Connection connection;

	public DocenteCrudDao() {
		connection = DBUtility.getConnection();
	}

	/**
	 * Retorna todos los docentes
	 * 
	 * @return List<Materia>
	 */
	public List<Docente> getAllDocentes() {
		List<Docente> docenteList = new ArrayList<Docente>();
		try {
			ResultSet rs = null;
			CallableStatement cs = null;
			cs = connection
					.prepareCall("{call SP_TBL_DOCENTE(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)}");
			// No se para que sirven
			FirebirdCallableStatement fbCs = (FirebirdCallableStatement) cs;
			fbCs.setSelectableProcedure(true);
			// -------------

			if (cs != null) {
				cs.registerOutParameter(1, Types.INTEGER);
				cs.registerOutParameter(2, Types.VARCHAR);
				cs.registerOutParameter(3, Types.VARCHAR);
				cs.registerOutParameter(4, Types.INTEGER);
				cs.registerOutParameter(5, Types.VARCHAR);
				cs.registerOutParameter(6, Types.VARCHAR);
				cs.registerOutParameter(7, Types.VARCHAR);
				cs.registerOutParameter(8, Types.VARCHAR);
				cs.registerOutParameter(9, Types.INTEGER);
				cs.registerOutParameter(10, Types.VARCHAR);
				cs.registerOutParameter(11, Types.VARCHAR);
				cs.registerOutParameter(12, Types.VARCHAR);
				cs.registerOutParameter(13, Types.VARCHAR);
				cs.registerOutParameter(14, Types.VARCHAR);
				cs.registerOutParameter(15, Types.VARCHAR);
				cs.registerOutParameter(16, Types.VARCHAR);

				rs = cs.executeQuery();

				while (rs.next()) {
					Docente dc = new Docente();

					dc.setLegajo(rs.getInt("legajo"));
					dc.setApenom(rs.getString("apeNom"));
					dc.setTdoc(rs.getString("tDoc"));
					dc.setNrodoc(rs.getInt("nroDoc"));
					dc.setCargo(rs.getString("cargo"));
					dc.setSituacionr(rs.getString("situacionR"));
					dc.setArea(rs.getString("area"));
					dc.setFecha_nac(rs.getString("fecha_nac"));
					dc.setCp(rs.getString("cp"));
					dc.setLocalidad(rs.getString("localidad"));
					dc.setDir(rs.getString("dir"));
					dc.setTel(rs.getString("tel"));
					dc.setCel(rs.getString("cel"));
					dc.setEmail(rs.getString("email"));
					dc.setObs(rs.getString("obs"));
					dc.setTitularen(rs.getString("titularEn"));

					docenteList.add(dc);
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (Exception e) {
			System.out.println("Erroe en getStudentsByIdm " + e.toString());
			e.printStackTrace();
		}

		return docenteList;
	}
}
